package com.code.util;

import com.code.generator.model.ColumnData;
import com.code.util.StringUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TransformUtils {
    public TransformUtils() {
    }

    public static String transformColumnTypeToFieldType(int type) {
        switch(type) {
        case -16:
        case 1:
        case 12:
            return "String";
        case -7:
            return "Boolean";
        case -6:
            return "Byte";
        case -5:
            return "Long";
        case -4:
        case -3:
        case -2:
            return "byte[]";
        case 2:
        case 3:
            return "java.math.BigDecimal";
        case 4:
            return "Integer";
        case 5:
            return "Short";
        case 6:
            return "Double";
        case 7:
            return "Float";
        case 8:
            return "Double";
        case 91:
            return "java.util.Date";
        case 92:
            return "java.util.Date";
        case 93:
            return "java.util.Date";
        case 2004:
            return "byte[]";
        case 2005:
            return "String";
        default:
            return "Unknown";
        }
    }

    public static String getColumnType(int type) {
        switch(type) {
        case -16:
            return "LONGNVARCHAR";
        case -7:
            return "BIT";
        case -6:
            return "TINYINT";
        case -5:
            return "BIGINT";
        case -4:
            return "LONGVARBINARY";
        case -3:
            return "VARBINARY";
        case -2:
            return "BINARY";
        case 1:
            return "CHAR";
        case 2:
            return "NUMERIC";
        case 3:
            return "DECIMAL";
        case 4:
            return "INTEGER";
        case 5:
            return "SMALLINT";
        case 6:
            return "FLOAT";
        case 7:
            return "REAL";
        case 8:
            return "DOUBLE";
        case 12:
            return "VARCHAR";
        case 91:
            return "DATE";
        case 92:
            return "TIME";
        case 93:
            return "TIMESTAMP";
        case 2004:
            return "BLOB";
        case 2005:
            return "CLOB";
        default:
            return "Unknown";
        }
    }

    public static String transformColumnNameToFieldName(String label) {
        if(Character.isUpperCase(label.indexOf(0))) {
            label = label.toLowerCase();
        }

        String[] array = label.split("_");
        StringBuilder s = new StringBuilder();
        boolean isFirst = true;
        String[] var7 = array;
        int var6 = array.length;

        for(int var5 = 0; var5 < var6; ++var5) {
            String str = var7[var5];
            if(isFirst) {
                s.append(str.substring(0, 1).toLowerCase());
            } else {
                s.append(str.substring(0, 1).toUpperCase());
            }

            s.append(str.substring(1));
            isFirst = false;
        }

        return s.toString();
    }

    public static String getJdbcDataType(String dataType, String precision, String scale) {
        dataType = dataType.toLowerCase();
        if(dataType.contains("char")) {
            dataType = "VARCHAR";
        } else if(dataType.contains("text")) {
            dataType = "VARCHAR";
        } else if(dataType.contains("bigint")) {
            dataType = "BIGINT";
        } else if(dataType.contains("int")) {
            dataType = "INTEGER";
        } else if(dataType.contains("float")) {
            dataType = "DECIMAL";
        } else if(dataType.contains("double")) {
            dataType = "DECIMAL";
        } else if(dataType.contains("number")) {
            if(StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
                dataType = "DECIMAL";
            } else if(StringUtils.isNotBlank(precision) && Integer.parseInt(precision) > 9) {
                dataType = "BIGINT";
            } else {
                dataType = "INTEGER";
            }
        } else if(dataType.contains("decimal")) {
            dataType = "DECIMAL";
        } else if(dataType.contains("date")) {
            dataType = "DATE";
        } else if(dataType.contains("time")) {
            dataType = "TIMESTAMP";
        } else if(dataType.contains("clob")) {
            dataType = "CLOB";
        } else {
            dataType = "OBJECT";
        }

        return dataType;
    }

    public static String getType(String dataType, String precision, String scale) {
        dataType = dataType.toLowerCase();
        if(dataType.contains("char")) {
            dataType = "String";
        } else if(dataType.contains("text")) {
            dataType = "String";
        } else if(dataType.contains("bigint")) {
            dataType = "Long";
        } else if(dataType.contains("int")) {
            dataType = "Integer";
        } else if(dataType.contains("float")) {
            dataType = "Float";
        } else if(dataType.contains("double")) {
            dataType = "Double";
        } else if(dataType.contains("number")) {
            if(StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
                dataType = "BigDecimal";
            } else if(StringUtils.isNotBlank(precision) && Integer.parseInt(precision) > 9) {
                dataType = "Long";
            } else {
                dataType = "Integer";
            }
        } else if(dataType.contains("decimal")) {
            dataType = "BigDecimal";
        } else if(dataType.contains("date")) {
            dataType = "Date";
        } else if(dataType.contains("time")) {
            dataType = "Date";
        } else if(dataType.contains("clob")) {
            dataType = "Clob";
        } else {
            dataType = "Object";
        }

        return dataType;
    }

    public static String getcolumnNameToDomainPropertyName(String columnName) {
        columnName = columnName.toLowerCase();
        String[] split = columnName.split("_");
        if(split.length <= 1) {
            columnName = split[0].substring(0, 1).toLowerCase() + split[0].substring(1, split[0].length());
            return columnName;
        } else {
            StringBuffer sb = new StringBuffer();

            for(int i = 0; i < split.length; ++i) {
                String tempTableName = split[i].substring(0, 1).toUpperCase() + split[i].substring(1, split[i].length());
                sb.append(tempTableName);
            }

            columnName = sb.toString();
            columnName = columnName.substring(0, 1).toLowerCase() + columnName.substring(1, columnName.length());
            return columnName;
        }
    }

    public static String getTablesASName(String tableName) {
        tableName = tableName.toLowerCase();
        String[] split = tableName.split("_");
        if(split.length <= 1) {
            String var5 = split[0].substring(0, 1).toLowerCase();
            return var5;
        } else {
            StringBuffer tempTables = new StringBuffer();

            for(int i = 0; i < split.length; ++i) {
                String tempTableName = split[i].substring(0, 1).toLowerCase();
                tempTables.append(tempTableName);
            }

            return tempTables.toString();
        }
    }

    public static String getColumnKeySort(List<ColumnData> columnList) {
        StringBuffer sb = new StringBuffer("");
        if(columnList != null && columnList.size() > 0) {
            Iterator iterator = columnList.iterator();

            while(iterator.hasNext()) {
                ColumnData str = (ColumnData)iterator.next();
                sb.append(str.getColumnName()).append(",");
            }
        }

        String str1 = sb.toString();
        if(str1.length() > 0) {
            str1 = str1.substring(0, str1.length() - 1);
        }

        return str1;
    }

    public static String getColumnKeyParam(List<ColumnData> columnList) {
        StringBuffer sb = new StringBuffer("");
        if(columnList != null && columnList.size() > 0) {
            Iterator iterator = columnList.iterator();

            while(iterator.hasNext()) {
                ColumnData str = (ColumnData)iterator.next();
                sb.append(str.getDataType()).append(" ").append(str.getDomainPropertyName()).append(",");
            }
        }

        String str1 = sb.toString();
        if(str1.length() > 0) {
            str1 = str1.substring(0, str1.length() - 1);
        }

        return str1;
    }

    public static String getColumnKeyParamValue(List<ColumnData> columnList, String splitChar) {
        StringBuffer sb = new StringBuffer("");
        if(columnList != null && columnList.size() > 0) {
            Iterator iterator = columnList.iterator();

            while(iterator.hasNext()) {
                ColumnData str = (ColumnData)iterator.next();
                sb.append(str.getDomainPropertyName()).append(splitChar);
            }
        }

        String str1 = sb.toString();
        if(str1.length() > 0) {
            str1 = str1.substring(0, str1.length() - 1);
        }

        return str1;
    }

    public static String getColumnKeyParamValuePrint(List<ColumnData> columnList) {
        StringBuffer sb = new StringBuffer("");
        if(columnList != null && columnList.size() > 0) {
            Iterator iterator = columnList.iterator();

            while(iterator.hasNext()) {
                ColumnData str = (ColumnData)iterator.next();
                sb.append("\"").append(str.getDomainPropertyName()).append("=\"+").append(str.getDomainPropertyName()).append("+");
            }
        }

        String str1 = sb.toString();
        if(str1.length() > 0) {
            str1 = str1.substring(0, str1.length() - 1);
        }

        return str1;
    }

    public static String getTablesNameToClassName(String tableName) {
        tableName = tableName.toLowerCase();
        String[] split = tableName.split("_");
        if(split.length <= 1) {
            String var5 = split[0].substring(0, 1).toUpperCase() + split[0].substring(1, split[0].length());
            return var5;
        } else {
            StringBuffer tempTables = new StringBuffer();

            for(int i = 0; i < split.length; ++i) {
                String tempTableName = split[i].substring(0, 1).toUpperCase() + split[i].substring(1, split[i].length());
                tempTables.append(tempTableName);
            }

            return tempTables.toString();
        }
    }

    public static List<ColumnData> getColumnKeyDatas(List<ColumnData> columnList) {
        ArrayList columnKeyList = new ArrayList();
        if(columnList != null && columnList.size() > 0) {
            Iterator iterator = columnList.iterator();

            while(iterator.hasNext()) {
                ColumnData column = (ColumnData)iterator.next();
                if(column.isKeyColumn()) {
                    columnKeyList.add(column);
                }
            }
        }

        return columnKeyList;
    }
}
